package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import zhang.lao.build.kit.LogKit;
import zhang.lao.dao.base.WnFarmlandDao;
import zhang.lao.build.mybatis.jdbc.auto.model.WnFarmland;
import zhang.lao.build.mybatis.jdbc.auto.model.WnFarmlandExample;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.FarmLandResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;

import static javafx.scene.input.KeyCode.F;

/**
* 
* @author 
* @create 
**/
 @Service
public class WnFarmlandService{
	@Resource
	private WnFarmlandDao wnFarmlandDao;

	public String add(){
		return "console/wnFarmland/wnFarmland_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
			modelMap.put("wnFarmland", wnFarmlandDao.selectByPrimaryKey(id));
		return "console/wnFarmland/wnFarmland_form";
	}

	public String list(){
		return "console/wnFarmland/wnFarmland_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq, ConsoleContext consoleContext){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnFarmlandExample wnFarmlandExample = new WnFarmlandExample();
        ControllerQueryTool.setWnFarmlandCriteria(bootGridReq.getQuery(),wnFarmlandExample.createCriteria()).andFarmIdIn(consoleContext.getFarms());
		List<WnFarmland> wnFarmlandList = wnFarmlandDao.selectByExample(wnFarmlandExample);
		return new BootStrapGridResp(page.getTotal(),wnFarmlandList);
	}

	public HttpResult showJson( String farmIds,ConsoleContext consoleContext){
		WnFarmlandExample wnFarmlandExample = new WnFarmlandExample();
		if(StringUtils.isBlank(farmIds)){
			wnFarmlandExample.createCriteria().andFarmIdIn(consoleContext.getFarms());
		}else{
			wnFarmlandExample.createCriteria().andFarmIdIn(Lists.newArrayList(farmIds.split(",")));
		}

		 List<FarmLandResp> landRespArrayList = Lists.newArrayList();
		List<WnFarmland> wnFarmlandList = wnFarmlandDao.selectByExample(wnFarmlandExample);
		if(wnFarmlandList!=null){
			for (WnFarmland wnFarmland : wnFarmlandList) {
				FarmLandResp farmLandResp = new FarmLandResp();
				farmLandResp = JSON.parseObject(wnFarmland.getCenterLatlng(),FarmLandResp.class);
				List<FarmLandResp> landResps = JSONArray.parseArray(wnFarmland.getLatlngs(),FarmLandResp.class);
					farmLandResp.setList(landResps);
				farmLandResp.setName(wnFarmland.getName());
				farmLandResp.setNumber(wnFarmland.getFarmlandNumber());
				farmLandResp.setAddress(wnFarmland.getAddress());
				landRespArrayList.add(farmLandResp);
			}
		}

		return HttpResultUtil.buildSuccess(landRespArrayList);
	}


	public HttpResult save(String formObjectJson){
		WnFarmland wnFarmland= JSON.parseObject(formObjectJson,WnFarmland.class);
			java.lang.String id=wnFarmland.getId();
		if (id!=null) {
			wnFarmlandDao.updateByPrimaryKeySelective(wnFarmland);
		}else{
			wnFarmland.setId(UUIDTool.getUUID());
			wnFarmlandDao.insertSelective(wnFarmland);
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		wnFarmlandDao.deleteByPrimaryKey(java.lang.String.valueOf(id));
		}
		return HttpResultUtil.buildSuccess();
	}
}
